package com.squid.cloud.common.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.cglib.beans.BeanMap;

public class MapUtils {

	/**
	 * 默认按照ASCLL排序，value有双引号
	 * @param map
	 * @param glue
	 * @return
	 */
	public static String joinOrderHaveDoubleQuotes(Map<String, String> map, String glue){
		return join(map, glue, String.CASE_INSENSITIVE_ORDER, true);
	}
	
	/**
	 * 默认按照ASCLL排序，value不带双引号
	 * @param map
	 * @param glue
	 * @return
	 */
	public static String joinOrder(Map<String, String> map, String glue){
		return join(map, glue, String.CASE_INSENSITIVE_ORDER, false);
	}
	
	/**
	 * 不排序，value有双引号
	 * @param map
	 * @param glue
	 * @return
	 */
	public static String joinNoOrderHaveDoubleQuotes(Map<String, String> map, String glue){
		return join(map, glue, null, true);
	}
	
	/**
	 * 不排序，value不带双引号
	 * @param map
	 * @param glue
	 * @return
	 */
	public static String joinNoOrder(Map<String, String> map, String glue){
		return join(map, glue, null, false);
	}
	
	/**
	 * 将MAP的元素按照指定表示拼接
	 * @param map
	 * @param glue
	 * @param orderRule
	 * @return
	 */
	public static String join(Map<String, String> map, String glue, Comparator<String> orderRule, boolean haveDoubleQuotes){
		if(map == null || map.size() == 0){
			return "";
		}
        ArrayList<String> list = new ArrayList<String>();
        for(Map.Entry<String, String> entry:map.entrySet()){
        	if(haveDoubleQuotes){
        		list.add(entry.getKey() + "=\"" + entry.getValue().toString() + "\"");
        	}else{
        		list.add(entry.getKey() + "=" + entry.getValue().toString());
        	}
        }
        int size = list.size();
        String [] arrayToSort = list.toArray(new String[size]);
        if(orderRule != null){
        	Arrays.sort(arrayToSort, orderRule);
        }
        return StringUtils.join(glue, arrayToSort);
	}
	
	/**
	 * 将对象装换为map
	 * 
	 * @param bean
	 * @return
	 */
	public static <T> Map<String, Object> beanToMap(T bean) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (bean != null) {
			BeanMap beanMap = BeanMap.create(bean);
			for (Object key : beanMap.keySet()) {
				map.put(key + "", beanMap.get(key));
			}
		}
		return map;
	}
	
	/**
	 * 将对象转换为map，排除空值
	 * @param bean
	 * @return
	 */
	public static <T> Map<String, Object> beanToMapExcludeEmpty(T bean) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (bean != null) {
			BeanMap beanMap = BeanMap.create(bean);
			for (Object key : beanMap.keySet()) {
				if(beanMap.get(key) != null && StringUtils.isNotEmpty(beanMap.get(key).toString())){
					map.put(key + "", beanMap.get(key));
				}
			}
		}
		return map;
	}
	
	/**
	 * 将对象装换为map，null转为空字符串
	 * @param bean
	 * @return
	 */
	public static <T> Map<String, String> beanToStringMap(T bean) {
		Map<String, String> map = new HashMap<String, String>();
		if (bean != null) {
			BeanMap beanMap = BeanMap.create(bean);
			for (Object key : beanMap.keySet()) {
				if(beanMap.get(key) == null){
					map.put(key + "", "");
				}else{
					map.put(key + "", beanMap.get(key).toString());
				}
			}
		}
		return map;
	}
	
	/**
	 * 将对象转换为map，排除空值
	 * @param bean
	 * @return
	 */
	public static <T> Map<String, String> beanToStringMapExcludeEmpty(T bean) {
		Map<String, String> map = new HashMap<String, String>();
		if (bean != null) {
			BeanMap beanMap = BeanMap.create(bean);
			for (Object key : beanMap.keySet()) {
				if(beanMap.get(key) != null && StringUtils.isNotEmpty(beanMap.get(key).toString())){
					map.put(key + "", beanMap.get(key).toString());
				}
			}
		}
		return map;
	}

	/**
	 * 将map装换为javabean对象
	 * 
	 * @param map
	 * @param bean
	 * @return
	 */
	public static <T> T mapToBean(Map<String, Object> map, T bean) {
		BeanMap beanMap = BeanMap.create(bean);
		beanMap.putAll(map);
		return bean;
	}
	
	/**
	 * 将map装换为javabean对象
	 * 
	 * @param map
	 * @param bean
	 * @return
	 */
	public static <T> T mapStringToBean(Map<String, String> map, T bean) {
		BeanMap beanMap = BeanMap.create(bean);
		beanMap.putAll(map);
		return bean;
	}

	/**
	 * 将List<T>转换为List<Map<String, Object>>
	 * 
	 * @param objList
	 * @return
	 * @throws IOException
	 */
	public static <T> List<Map<String, Object>> objectsToMaps(List<T> objList) {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		if (objList != null && objList.size() > 0) {
			Map<String, Object> map = null;
			T bean = null;
			for (int i = 0, size = objList.size(); i < size; i++) {
				bean = objList.get(i);
				map = beanToMap(bean);
				list.add(map);
			}
		}
		return list;
	}

	/**
	 * 将List<Map<String,Object>>转换为List<T>
	 * 
	 * @param maps
	 * @param clazz
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	public static <T> List<T> mapsToObjects(List<Map<String, Object>> maps,
			Class<T> clazz) throws InstantiationException, IllegalAccessException {
		List<T> list = new ArrayList<T>();
		if (maps != null && maps.size() > 0) {
			Map<String, Object> map = null;
			T bean = null;
			for (int i = 0, size = maps.size(); i < size; i++) {
				map = maps.get(i);
				bean = clazz.newInstance();
				mapToBean(map, bean);
				list.add(bean);
			}
		}
		return list;
	}
	
	/**
	 * 判断map是否为空
	 * @param collection
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static boolean isEmpty(Map map){
		if(map == null || map.size() == 0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 判断map是否不为空
	 * @param collection
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static boolean isNotEmpty(Map map){
		return !isEmpty(map);
	}
	
}
